<template>
    <div>
        <el-dialog v-model="dialogFormVisible" title="条件选择" width="500">
            <el-form :model="exportForm" :rules="rules" ref="ruleFormRef">
                <el-form-item label="公司:" :label-width="100">
                    <el-select class="w-full" clearable v-model="exportForm.companyId" filterable remote reserve-keyword
                        placeholder="请选择公司" :remote-method="remoteMethod" :loading="loading">
                        <el-option v-for="item in companylist" :key="item.id" :label="item.companyName"
                            :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="产品类型:" :label-width="100">
                    <el-radio-group v-model="exportForm.cplx" @change="changeproducttypeInDia">
                        <el-radio value="1">日日保</el-radio>
                        <el-radio value="2">长期保</el-radio>
                        <el-radio value="3">团意保</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="保司:" :label-width="100" prop="bs">
                    <el-select v-model="exportForm.bs" clearable placeholder="请选择">
                        <el-option v-for="(ele, index) in baosilistDia" :key="ele.dictValue" :label="ele.dictLabel"
                            :value="ele.dictValue" />
                    </el-select>
                </el-form-item>
                <el-form-item prop="month" label="选择月份:" :label-width="100">
                    <el-date-picker clearable v-model="exportForm.month" class="w-full" value-format="YYYY-MM"
                        type="month" placeholder="请选择" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="flex items-center w-full justify-center">
                    <el-button @click="diaExportCancel">取消</el-button>
                    <el-button type="primary" @click="diaExportSure(ruleFormRef)">
                        确定
                    </el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup lang="ts">
import { exportdaykhlbapi, exportlongkhlbapi, getcompanyapi, geteverydayinsuranceaudicompanytapi, getlongtimeinsuranceaudicompanytapi } from '@/api/api'
import baseurl from '@/api/env'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { reactive, ref, watch } from 'vue'
const props = defineProps({
    show: {
        type: Boolean,
        default: false
    }
})
const rules = reactive<FormRules>({
    bs: [
        {
            required: true,
            message: '请选择保司',
            trigger: 'change',
        },
    ],
    month: [
        {
            required: true,
            message: '请选择月份',
            trigger: 'change',
        },
    ],
})
const ruleFormRef = ref<FormInstance>()
const exportForm = ref({
    companyId: '',
    cplx: '',
    bs: '',
    month: '',
})
const baosilistDia = ref<any>([])
const emits = defineEmits(['update:show'])
const companylist = ref([] as any)
const dialogFormVisible = ref(false)
const loading = ref(false)
const remoteMethod = (query: string) => {
    console.log(query)
    if (query) {
        loading.value = true
        getcompany(query)
    } else {
        companylist.value = []
    }
}
const getcompany = async (val: any) => {
    const res: any = await getcompanyapi(val)
    console.log(res, '获取公司列表')
    companylist.value = res
    const statusMap = {
        0: '待审核',
        1: '审核通过',
        2: '审核驳回',
    } as any
    companylist.value.forEach((i: any) => {
        i.companyName =
            i.companyName +
            (i.auditStatus || i.auditStatus === 0
                ? `（${statusMap[i.auditStatus]}）`
                : '')
    })
    loading.value = false
}
const getlongtimeinsuranceaudicompanyt = async () => {
    const res: any = await getlongtimeinsuranceaudicompanytapi()
    baosilistDia.value = res

}
const geteverydayinsuranceaudicompanyt = async () => {
    const res: any = await geteverydayinsuranceaudicompanytapi()
    baosilistDia.value = res
}
const diaExportSure = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate(async (valid, fields) => {
        if (valid) {
            if (exportForm.value.cplx === '1') {
                exportdaykhlbapi({
                    queryMonth: exportForm.value.month,
                    insureCompanyType: exportForm.value.bs,
                    companyId: exportForm.value.companyId,
                }).then((res: any) => {
                    console.log(res)
                    window.location.href =
                        baseurl + '/common/download?delete=true&fileName=' + res
                    success()
                })
                return
            }
            if (exportForm.value.cplx === '3') {
                exportdaykhlbapi({
                    queryMonth: exportForm.value.month,
                    insureCompanyType: exportForm.value.bs,
                    companyId: exportForm.value.companyId,
                }).then((res: any) => {
                    console.log(res)
                    window.location.href =
                        baseurl + '/common/download?delete=true&fileName=' + res
                    success()
                })
                return
            }
            if (exportForm.value.cplx === '2') {
                exportlongkhlbapi({
                    queryMonth: exportForm.value.month,
                    insureCompanyType: exportForm.value.bs,
                    companyId: exportForm.value.companyId,
                }).then((res: any) => {
                    console.log(res)
                    window.location.href =
                        baseurl + '/common/download?delete=true&fileName=' + res
                    success()
                })
                return
            }
            await diaExportCancel()
        } else {
            console.log('error submit!', fields)
        }
    })
}
const success = async () => {
    await ElMessage({
        message: '导出成功',
        type: 'success',
    })
}
const diaExportCancel = async () => {
    exportForm.value.cplx = '1'
    exportForm.value.bs = ''
    exportForm.value.month = ''
    await geteverydayinsuranceaudicompanyt()
    dialogFormVisible.value = false
}
const changeproducttypeInDia = (val: any) => {
    exportForm.value.bs = ''
    if (val === '') {
        baosilistDia.value = []
    }
    if (val === '1') {
        geteverydayinsuranceaudicompanyt()
    }
    if (val === '2') {
        getlongtimeinsuranceaudicompanyt()
    }
    if (val === '3') {
        exportForm.value.month = ''
        geteverydayinsuranceaudicompanyt()
    }
}
watch(() => props.show, async (newval) => {
    if (newval) {
        await diaExportCancel()
    }
    dialogFormVisible.value = newval
})
watch(dialogFormVisible, (newval) => {
    emits('update:show', newval)
    if (!newval) {
        ruleFormRef.value?.resetFields()
    }
})
</script>

<style scoped></style>